package com.example.pdfandword.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.*;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.HmacUtils;
import org.codehaus.jettison.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class fhlv
{


    public static String fhlvpdf(String orderNo){
        String res = "";

        try{
        String accessKeyId = "BAtQR6tyCFUxA4PkGioERe";
        String accessKeySecret = "J88X9xSjp1vgix3epNLNET";
        Map<String, String> params = new HashMap<>();
        params.put("id", orderNo);
        ObjectMapper objectMapper = new ObjectMapper();
        String inputJson = objectMapper.writeValueAsString(params);
        byte[] bytes = HmacUtils.hmacSha1(accessKeySecret, inputJson);
        String signature = Base64.encodeBase64String(bytes);

        OkHttpClient client = new OkHttpClient()
                .newBuilder()
                .readTimeout(30, TimeUnit.SECONDS)
                .build();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(mediaType, inputJson);
        Request.Builder builder = new Request.Builder()
                .url("http://oapi.fhldtech.com/report-export/v1/pdf");
        builder.addHeader("x-fhld-access-key-id", accessKeyId);
        builder.addHeader("x-fhld-signature", signature);
        Request request = builder.post(body).build();
        Response response = client.newCall(request).execute();
        res = response.body().string();
        }catch (Exception e){

        }
        return  res;
    }

    public static String fhlvquery(String name,String idnumber,String phoneNumber){
        String res = "";
        try{
            String accessKeyId = "BAtQR6tyCFUxA4PkGioERe";
            String accessKeySecret = "J88X9xSjp1vgix3epNLNET";
            Map<String, String> params = new HashMap<>();
            params.put("name", name);
            params.put("idNumber", idnumber);
            params.put("phoneNumber", phoneNumber);
            ObjectMapper objectMapper = new ObjectMapper();
            String inputJson = objectMapper.writeValueAsString(params);
            byte[] bytes = HmacUtils.hmacSha1(accessKeySecret, inputJson);
            String signature = Base64.encodeBase64String(bytes);
            OkHttpClient client = new OkHttpClient()
                    .newBuilder()
                    .readTimeout(30, TimeUnit.SECONDS)
                    .build();
            MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
            RequestBody body = RequestBody.create(mediaType, inputJson);
            Request.Builder builder = new Request.Builder()
                    .url("http://oapi.fhldtech.com/person-risk/v1/comprehensive-assessment");
            builder.addHeader("x-fhld-access-key-id", accessKeyId);
            builder.addHeader("x-fhld-signature", signature);
            Request request = builder.post(body).build();
            Response response = client.newCall(request).execute();
            res =response.body().string() ;
        }catch (Exception e){

        }


        return res;

    }


}
